package com.opages.netty.demo.lesson.dark_horse3;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author daizhong.liu
 * @create 2022-01-04 10:49
 **/
@Slf4j
public class NettyServer {
    public void simpleNettyServer() {
        ChannelFuture future = new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                ByteBuf buf = (ByteBuf) msg;
                                log.debug("{}:msg={}",Thread.currentThread().getName(),buf.toString(CharsetUtil.UTF_8));
                            }
                        });
                    }
                }).bind(8889);
        future.addListener((listen)->{
            if(listen.isSuccess()) {
                log.debug("服务端启动成功...");
            }
        });
    }

    public void groupServer() {
        ChannelFuture future = new ServerBootstrap()
                .group(new NioEventLoopGroup(1), new NioEventLoopGroup(2))
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("name={},msg={}",Thread.currentThread().getName(),((ByteBuf)msg).toString(CharsetUtil.UTF_8));
                                ctx.fireChannelRead(msg);
                            }
                        }).addLast(new NioEventLoopGroup(3), "new_group", new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("name={},msg={}",Thread.currentThread().getName(),((ByteBuf)msg).toString(CharsetUtil.UTF_8));
                            }
                        });
                    }
                }).bind(8889);
        future.addListener((listen)->{
            if(listen.isSuccess()) {
                log.debug("启动成功!");
            }
        });
    }

    public static void main(String[] args) {
        new NettyServer().groupServer();
    }
}
